﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    public class ExamresultsTestQuestionDAL
    {
        internal static ObservableCollection<ExamresultsTestQuestionModel> GetTestQuestionByExamResultsID(int examResultsID, bool isSingle = true)
        {
            //北京科研修改 副场景ID对不上，examResultsID改为ID  modfiy by ljz
            string sql = "SELECT * FROM examresultstestquestion where ExamResultsID = " + examResultsID + " and IsMain=1";
            DataSet ds = DbHelper.ExecuteDataset(sql);

            ObservableCollection<ExamresultsTestQuestionModel> list = new ObservableCollection<ExamresultsTestQuestionModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetTestQuestionModel(dt.Rows[i], isSingle));
                }
            }
            return list;
        }
        public static String SelectTestquestionName(int testId)
        {
            String sql = "SELECT Name FROM testquestion where TestQuestionID=" + testId;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return dt.Rows[0][0].ToString();
            }
            return "";
        }

        internal static DataSet GetTestQuestion(int examResultsID)
        {
            string sql = "SELECT a.*,b.Describe,b.rule,b.countdown FROM examresultstestquestion a inner join testquestion b on a.testquestionid=b.testquestionid where a.ExamResultsID = " + examResultsID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            return ds;
        }

        public static ExamresultsTestQuestionModel GetQuestionByID(int testQuestionId)
        {
            string sql = "select * from examresultstestquestion where ID = " + testQuestionId;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            ExamresultsTestQuestionModel info = null;
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    info = GetTestQuestionModel(dt.Rows[i]);
                }
            }
            return info;
        }

        public static bool UpdateExamresultsTestQuestion(ExamresultsTestQuestionModel examresultsTestQuestion)
        {
            string sql = "update examresultstestquestion set TotalScore=@TotalScore,ResultScore=@ResultScore,TimeScore=@TimeScore,SubjectiveScore=@SubjectiveScore,OperationScore=@OperationScore,SubmissionTime=@SubmissionTime,OtherScore=@OtherScore WHERE ExamResultsTestQuestionID = @ID";

            MySqlParameter[] parms = {
                new MySqlParameter("@TotalScore", examresultsTestQuestion.TotalScore),
                //new MySqlParameter("@ResultScore", examresultsTestQuestion.ResultScore),
                //new MySqlParameter("@OtherComprehensiveScore", examresultsTestQuestion.OtherComprehensiveScore),
                //new MySqlParameter("@TaskTotalScore", examresultsTestQuestion.TaskTotalScore),
                //new MySqlParameter("@TimeScore", examresultsTestQuestion.TimeScore),
                //new MySqlParameter("@SubjectiveScore", examresultsTestQuestion.SubjectiveScore),
                //new MySqlParameter("@OperationScore", examresultsTestQuestion.OperationScore),
                //new MySqlParameter("@SubmissionTime", examresultsTestQuestion.SubmissionTime),
                //new MySqlParameter("@OtherScore", examresultsTestQuestion.OtherScore),
                new MySqlParameter("@ID", examresultsTestQuestion.ID)
            };

            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }
        public static bool UpdateExamresultsTestQuestionOther(ExamresultsTestQuestionModel examresultsTestQuestion)
        {
            string sql = "update examresultstestquestion set SubjectiveScore=@SubjectiveScore,SubmissionTime=@SubmissionTime,OtherSubjectiveScore=@OtherSubjectiveScore,TotalScore=@TotalScore WHERE ExamResultsTestQuestionID = @ID";

            MySqlParameter[] parms = {
                //new MySqlParameter("@SubjectiveScore", examresultsTestQuestion.SubjectiveScore),
                //new MySqlParameter("@SubmissionTime", examresultsTestQuestion.SubmissionTime),
                //new MySqlParameter("@OtherSubjectiveScore", examresultsTestQuestion.OtherSubjectiveScore),
                new MySqlParameter("@TotalScore", examresultsTestQuestion.TotalScore),
                new MySqlParameter("@ID", examresultsTestQuestion.ID)
            };

            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        private static ExamresultsTestQuestionModel GetTestQuestionModel(DataRow dr, bool isSingle = true)
        {
            ExamresultsTestQuestionModel info = new ExamresultsTestQuestionModel();

            info.ID = Convert.ToInt32(dr["ID"]);
            info.ExamResultsID = Convert.ToInt32(dr["ExamResultsID"]);
            info.TestQuestionID = Convert.ToInt32(dr["TestQuestionID"]);
            info.Name = dr["Name"].ToString();
            info.CountDown = Convert.ToInt32(dr["CountDown"]);
            info.Describe = dr["Describe"].ToString();

            if (dr["SceneFile"] != DBNull.Value)
                info.SceneFile = (byte[])dr["SceneFile"];
            if (dr["CreateTime"] != DBNull.Value)
                info.CreateTime = Convert.ToDateTime(dr["CreateTime"]);

            if (!(dr["TotalScore"] is DBNull))
                info.TotalScore = decimal.Parse(dr["TotalScore"].ToString());

            //if (!(dr["TestQuestionID"] is DBNull))
            //    info.TestQuestionID = decimal.Parse(dr["TestQuestionID"].ToString());

            if (!(dr["StopExplain"] is DBNull))
                info.StopExplain = (dr["StopExplain"].ToString());
            if (!(dr["DeductPointsExplain"] is DBNull))
                info.DeductPointsExplain = (dr["DeductPointsExplain"].ToString());
            if (!(dr["DeductPointsScore"] is DBNull))
                info.DeductPointsScore = decimal.Parse(dr["DeductPointsScore"].ToString());

            if (!(dr["StepScore"] is DBNull))
                info.StepScore = decimal.Parse(dr["StepScore"].ToString());
            if (!(dr["TrainLateCount"] is DBNull))
                info.TrainLateCount = decimal.Parse(dr["TrainLateCount"].ToString());
            if (!(dr["TrainSuspendCount"] is DBNull))
                info.TrainSuspendCount = decimal.Parse(dr["TrainSuspendCount"].ToString());
            if (!(dr["RecordtimeErrorCount"] is DBNull))
                info.RecordtimeErrorCount = decimal.Parse(dr["RecordtimeErrorCount"].ToString());
            if (!(dr["SurplusStep"] is DBNull))
                info.SurplusStep = int.Parse(dr["SurplusStep"].ToString());
            if (!(dr["EndTime"] is DBNull))
                info.EndTime = DateTime.Parse(dr["EndTime"].ToString());
            if (!(dr["IsMain"] is DBNull))
                info.IsMain = bool.Parse(dr["IsMain"].ToString());
            if (!(dr["AssistantQuestions"] is DBNull))
                info.AssistantQuestions = dr["AssistantQuestions"].ToString();
            if (!(dr["WaitCountDown"] is DBNull))
                info.WaitCountDown = int.Parse(dr["WaitCountDown"].ToString());
            if (!(dr["MinusPoints"] is DBNull))
                info.MinusPoints = dr["MinusPoints"].ToString();
            if (dr["StationID"] != DBNull.Value)
                info.StationID = int.Parse(dr["StationID"].ToString());
            if (dr["StopLaterScore"] != DBNull.Value)
                info.StopLaterScore = decimal.Parse(dr["StopLaterScore"].ToString());
            if (dr["SubmitElementScore"] != DBNull.Value)
                info.SubmitElementScore = decimal.Parse(dr["SubmitElementScore"].ToString());
            if (dr["TestPaperTotal"] != DBNull.Value)
                info.TestPaperTotal = decimal.Parse(dr["TestPaperTotal"].ToString());
            if (dr["StopsRunningCount"] != DBNull.Value)
                info.StopsRunningCount = decimal.Parse(dr["StopsRunningCount"].ToString());
            if (dr["StopsRunningTrain"] != DBNull.Value)
                info.StopsRunningTrain = dr["StopsRunningTrain"].ToString();
            if (isSingle)
                info.ExamresultsTestQuestionTasks = ExamresultsTestQuestionTaskDAL.GetQuestionTaskByExamResultsTestQuestion(info);

            return info;
        }

        public static ExamresultsTestQuestionModel getScentFile(int resultID)
        {
            string sql = "select * from examresultstestquestion where ID={0}";
           
            sql = string.Format(sql, resultID);
            DataSet ds = DbHelper.ExecuteDataset(sql);
            ExamresultsTestQuestionModel info = new ExamresultsTestQuestionModel();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                info = GetTestQuestionModel(dt.Rows[0]);
                return info;
            }
            return info;
        }
        public static String  getLevel(int resultID)
        {
            
            string sql = "SELECT*from testpaper where TestPaperID =(SELECT TestPaperID from testpaperquestion where TestQuestionID=" + resultID+")";
            DataSet ds = DbHelper.ExecuteDataset(sql);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                var data = dt.AsEnumerable().Select(c => c.Field<string>("Level")).ToList().FirstOrDefault();
                return data.ToString();
                
            }
            return "";
        }
        /// <summary>
        /// 更新考试试卷结果得分
        /// </summary>
        /// <param name="currentExamQuestion">当前试卷</param>
        public static void UpdateExamresultsTestQuestionScore(ExamresultsTestQuestionModel currentExamQuestion)
        {
            string sql = "update examresultstestquestion set " +
                "TotalScore=@TotalScore," +
                "StopExplain=@StopExplain," +
                "DeductPointsExplain=@DeductPointsExplain," +
                "DeductPointsScore=@DeductPointsScore," +
                "StepScore=@StepScore," +
                "TrainLateCount=@TrainLateCount," +
                "TrainSuspendCount=@TrainSuspendCount, " +
                "RecordtimeErrorCount=@RecordtimeErrorCount, " +
                "SurplusStep=@SurplusStep, " +
                "EndTime=@EndTime, " +
                "StopsRunningCount=@StopsRunningCount, " +
                
                "StopsRunningTrain=@StopsRunningTrain " +
                "WHERE ID = @ID";

            MySqlParameter[] parms = {
                new MySqlParameter("@TotalScore", currentExamQuestion.TotalScore),
                new MySqlParameter("@StopExplain", currentExamQuestion.StopExplain),
                new MySqlParameter("@DeductPointsExplain", currentExamQuestion.DeductPointsExplain),
                new MySqlParameter("@DeductPointsScore", currentExamQuestion.DeductPointsScore),
                new MySqlParameter("@StepScore", currentExamQuestion.StepScore),
                new MySqlParameter("@TrainLateCount", currentExamQuestion.TrainLateCount),
                new MySqlParameter("@TrainSuspendCount", currentExamQuestion.TrainSuspendCount),
                new MySqlParameter("@RecordtimeErrorCount", currentExamQuestion.RecordtimeErrorCount),
                new MySqlParameter("@SurplusStep", currentExamQuestion.SurplusStep),
                new MySqlParameter("@EndTime", currentExamQuestion.EndTime),
                
                new MySqlParameter("@StopsRunningCount", currentExamQuestion.StopsRunningCount),
                new MySqlParameter("@StopsRunningTrain", currentExamQuestion.StopsRunningTrain),
                new MySqlParameter("@ID", currentExamQuestion.ID)
            };

            DbHelper.ExecuteNonQuery(sql, parms);
        }
        //更新总分
        public static void UpdateExamresultsTestQuestionTestPaperScore(ExamresultsTestQuestionModel currentExamQuestion)
        {
            string sql = "update examresultstestquestion set TestPaperTotal=(select TestPaperTotal from testquestion where examresultstestquestion.TestQuestionID = testquestion.TestQuestionID) " +
                "WHERE ID = " + currentExamQuestion.ID;
            DbHelper.ExecuteNonQuery(sql);
        }
        public static void SetTestpaperTotal(ExamresultsTestQuestionModel currentExamQuestion,out Decimal dest)
        {
            string sql = "SELECT TotalScore FROM exam_testquestion where Id = " + currentExamQuestion.ID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            DataTable dt = ds.Tables[0];
            dest = decimal.Parse((dt.Rows[0][0]).ToString());
        }
        public static void UpdateExamresultsTestQuestionCreateTime(int iD, DateTime createTime)
        {
            string sql = "update examresultstestquestion set CreateTime=@CreateTime WHERE ID = @ID";

            MySqlParameter[] parms = {
                new MySqlParameter("@CreateTime",createTime ),
                new MySqlParameter("@ID", iD)
            };

            DbHelper.ExecuteNonQuery(sql, parms);
        }
    }
}
